package com.shungen.realapigateway.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.shungen.realapigateway.mapper.InterfaceInfoMapper;
import com.shungen.realapigateway.model.entity.InterfaceInfo;
import com.shungen.realapigateway.service.InterfaceInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shungen
 * @date 2024年08月13日 13:04
 */
@Service
public class InterfaceInfoServiceImpl implements InterfaceInfoService {

    @Resource
    private InterfaceInfoMapper interfaceInfoMapper;

    @Override
    public InterfaceInfo getInterfaceInfoById(Long interfaceId) {
        return interfaceInfoMapper.selectById(interfaceId);
    }

    @Override
    public List<InterfaceInfo> getAllInterface() {
        List<InterfaceInfo> interfaceInfoList = interfaceInfoMapper.selectList(null);
        return interfaceInfoList;
    }

    @Override
    public boolean incrementVisitedCount(Long interfaceId) {
        LambdaUpdateWrapper<InterfaceInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.setSql("visitedCount = visitedCount + 1") // 这里实现原子加1操作
                .eq(InterfaceInfo::getId, interfaceId); // 设定要更新的记录的条件
        InterfaceInfo interfaceInfo = new InterfaceInfo();interfaceInfo.setId(interfaceId);
        int update = interfaceInfoMapper.update(interfaceInfo, updateWrapper);
        return update == 1;
    }
}
